<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\StorePink;
use App\Traits\Controllers\IndexTraits;
use App\Traits\Controllers\ShowTraits;
use App\Transformers\Order\OrderTransformer;
use App\Transformers\Store\StorePinkTransformer;
use Carbon\Carbon;
use Illuminate\Http\Request;

class StorePinkController extends Controller
{
    //查看
    use IndexTraits;

    //详情
    use ShowTraits;

    public $model = StorePink::class;

    public $transformer = StorePinkTransformer::class;

    public function index(Request $request)
    {
        //先处理传过来的搜索数据
        $this->formatIndexQuery($request);
        //获取传过来的时间类型
        $time = $request->input('time');
        //获取开始时间和结束时间
        $start = $request->input('start');
        $stop = $request->input('stop');

        $data = $this
            ->getQuery()
            ->where('head_id',0)
            ->whereHas('storeCombination', function ($query) use ($time,$start,$stop) {
                //自定义时间
                if(!empty($start) && !empty($stop)){
                    $query->whereBetween('stop_at',[$start,$stop]);
                }

                //搜索今天数据
                if ($time == "today") {
                    $query->whereBetween('stop_at', [Carbon::now()->startOfDay(), Carbon::now()->endOfDay()]);
                }

                //搜索昨天数据
                if ($time == "yesterday") {
                    $query->whereDay('stop_at', [Carbon::now()->yesterday(), Carbon::now()->startOfDay()]);
                }

                //搜索最近一周数据
                if ($time == "week") {
                    $query->whereBetween('stop_at', [Carbon::now()->subDays(7), Carbon::now()->subDays(0)]);
                }

                //搜索最近三十天数据
                if ($time == "thirty") {
                    $query->whereBetween('stop_at', [Carbon::now()->subDays(30), Carbon::now()->subDays(0)]);
                }

                //搜索本月数据
                if ($time == "month") {
                    $query->whereBetween('stop_at', [Carbon::now()->startOfMonth(),Carbon::now()->endOfMonth()]);
                }

                //搜索本年数据
                if ($time == "year") {
                    $query->whereBetween('stop_at', [Carbon::now()->startOfYear(), Carbon::now()->endOfYear()]);
                }
            })
            ->pimp()//自动转换where条件
            ->withCount('storePink')
            ->paginate($this->getPageLimit());

        return $this->response->paginator($data, resolve($this->transformer));
    }

    /**
     * 查看所拼团下单的用户
     */

    public function show($id)
    {
        $data = $this->getQuery()
            ->select('id','user_id','is_refund','price')
            ->where('id',$id)
            ->with('user:id,avatar,nickname')
            ->with('order:pink_id,order_number')
            ->get()->toArray();
        $arr = $this->getQuery()
            ->select('id','user_id','is_refund','price')
            ->where('head_id',$id)
            ->with('user:id,avatar,nickname')
            ->with('order:pink_id,order_number')
            ->get()->toArray();
        foreach ($arr as $k => $v){
            array_push($data,$v);
        }

        return $this->response->array($data);
    }

    public function total()
    {
        //获取参与人数与成团数量数据
        $num = $this->getQuery()->selectRaw("COUNT(id) as totalPeople,COUNT(DISTINCT IF(status='completed',id,NULL)) as agglomerate")->first()->toArray();

        return $this->response->array($num);
    }
}
